package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.IntercomPlatformModelAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;

import java.util.Objects;

/**
 * Created by huanghaosheng on 2021/4/13 20:53
 */
@Service
@AttrHandler(IntercomPlatformModelAttrHandler.class)
public class IntercomPlatformModelAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        if (KiteStringUtils.isEqual(attr.getValue(), "110007203")) { //安卓语音对讲功能
            // 销售价
            operateInstAttrService.setAttrValue(inst, "200004107", "22", data.getSceneInstId(), null);
            // 结算价
            operateInstAttrService.setAttrValue(inst, "200004108", "20", data.getSceneInstId(), null);
            // 产品公允价
            operateInstAttrService.setAttrValue(inst, "200004115", "20", data.getSceneInstId(), null);
        }
        else if (KiteStringUtils.isEqual(attr.getValue(), "110007204")) { //安卓基础定位+调度功能
            // 销售价
            operateInstAttrService.setAttrValue(inst, "200004107", "17", data.getSceneInstId(), null);
            // 结算价
            operateInstAttrService.setAttrValue(inst, "200004108", "15", data.getSceneInstId(), null);
            // 产品公允价
            operateInstAttrService.setAttrValue(inst, "200004115", "15", data.getSceneInstId(), null);
        }
        else if (KiteStringUtils.isEqual(attr.getValue(), "110007205")) { //安卓高级巡更调度功能
            // 销售价
            operateInstAttrService.setAttrValue(inst, "200004107", "11", data.getSceneInstId(), null);
            // 结算价
            operateInstAttrService.setAttrValue(inst, "200004108", "10", data.getSceneInstId(), null);
            // 产品公允价
            operateInstAttrService.setAttrValue(inst, "200004115", "10", data.getSceneInstId(), null);
        }
        else if (KiteStringUtils.isEqual(attr.getValue(), "110007206")) { //安卓视频功能
            // 销售价
            operateInstAttrService.setAttrValue(inst, "200004107", "33", data.getSceneInstId(), null);
            // 结算价
            operateInstAttrService.setAttrValue(inst, "200004108", "30", data.getSceneInstId(), null);
            // 产品公允价
            operateInstAttrService.setAttrValue(inst, "200004115", "30", data.getSceneInstId(), null);
        }
        String rootInstId = querySceneInstService.getSceneInst(data.getSceneInstId()).getRootInstId();
        KiteOfferInst rootOfferInst = querySceneInstService.getOfferInst(data.getCustId(), rootInstId);
        String cateId = querySceneInstService.getOfferInstAttrValue(data.getCustId(), rootInstId, "cateId");
        if("2004109".equals(cateId) && "30000281".equals(rootOfferInst.getSpecId())){
            linkDealPrice(attr, inst, data);
        }
    }

    private void linkDealPrice(KiteAttr attr, KiteInst inst, AttrData data){
        //公允价
        KiteAttr fairPriceAttr = inst.getAttr("200004115");
        //销售价
        KiteAttr salePriceAttr = inst.getAttr("200004107");
        //结算价
        KiteAttr settlePriceAttr = inst.getAttr("200004108");

        if(Objects.nonNull(fairPriceAttr)){
            String fairPrice = AttrValueCache.getAttrValueByAttrValueName(fairPriceAttr.getAttrId(), attr.getValueDesc());
            operateInstAttrService.setAttrValue(inst,"200004115", fairPrice, data.getSceneInstId(),null);
        }
        if(Objects.nonNull(salePriceAttr)){
            String salePrice = AttrValueCache.getAttrValueByAttrValueName(salePriceAttr.getAttrId(), attr.getValueDesc());
            operateInstAttrService.setAttrValue(inst, "200004107", salePrice, data.getSceneInstId(), null);
        }
        if(Objects.nonNull(settlePriceAttr)){
            String settlePrice = AttrValueCache.getAttrValueByAttrValueName(settlePriceAttr.getAttrId(), attr.getValueDesc());
            operateInstAttrService.setAttrValue(inst, "200004108", settlePrice, data.getSceneInstId(), null);
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
